﻿<#
.SYNOPSIS  
    Enables web application features required for upgraded content databases to function correctly. 
.DESCRIPTION  
    
.NOTES
    If a feature is already enabled, it will be skipped.
.LINK  
    http://7up.codeplex.com 
#>

Write-Host ("   Enabling web application features ({0})..." -f [DateTime]::Now.ToString())

#add SharePoint snap in
if ((Get-PSSnapin -Name  Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) 
{ 
    Add-PsSnapin  Microsoft.SharePoint.PowerShell 
}

#get config
$config = [xml](Get-Content Upgrade.xml)

foreach($webApp in $config.Settings.WebApplications.WebApplication)
{
	Write-Host ("      Enabling web application features on '{0}' ({1})." -f $webApp.Url, [DateTime]::Now.ToString())
	foreach($featureToEnable in $webApp.FeaturesToEnable.FeatureToEnable)
	{
		if($featureToEnable -ne $null)
		{
			$theFeature = Get-SPFeature $featureToEnable.FeatureFolderName -WebApplication $webApp.Url -ErrorAction SilentlyContinue
			if($theFeature -eq $null)
			{
				Write-Host ("         Enabling feature '{0}' ({1})." -f $featureToEnable.FeatureFolderName, [DateTime]::Now.ToString())
				Enable-SPFeature $featureToEnable.FeatureFolderName -Url $webApp.Url
			}
			else
			{
				Write-Host ("         Skipping feature '{0}' because it's already enabled ({1})." -f $featureToEnable.FeatureFolderName, [DateTime]::Now.ToString())
			}
		}
	}
}
